using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Commerce.Common;
using System.Collections.Generic;
using SubSonic;

/// <summary>
/// Controller for business logic relating to Products
/// </summary>
public static class ProductRatingController
{
    /// <summary>
    /// Adds a rating to a product for a given user and resets the product's rating stats
    /// </summary>
    /// <param name="userName"></param>
    /// <param name="productID"></param>
    /// <param name="rating"></param>
    public static void AddUserRating(string userName, int productID, int rating)
    {

        SPs.StoreProductAddRating(productID, rating, userName).Execute();

    }

    /// <summary>
    /// Gets the user's rating for a products
    /// </summary>
    /// <param name="userName"></param>
    /// <param name="productID"></param>
    /// <returns></returns>
    public static int GetUserRating(string userName, int productID)
    {
        int iOut = -1;
        ProductRating rating = new ProductRating();
        rating.UserName = Utility.GetUserName();
        rating.ProductID = productID;
        IDataReader rdr = ProductRating.Find(rating);
        ProductRatingCollection coll = new ProductRatingCollection();
        coll.Load(rdr);
        rdr.Close();
        if (coll.Count > 0)
        {
            iOut = coll[0].Rating;
        }
        return iOut;

    }


}
